package be.bonamis.umpire.ui.client.activities;

import be.bonamis.projects.umpire.business.actions.GetAllQuizA;
import be.bonamis.projects.umpire.business.actions.GetAllQuizR;
import be.bonamis.projects.umpire.business.actions.GetClubsA;
import be.bonamis.projects.umpire.business.actions.GetClubsR;
import be.bonamis.projects.umpire.business.actions.GetGamesByUmpireA;
import be.bonamis.projects.umpire.business.actions.GetGamesByUmpireR;
import be.bonamis.projects.umpire.business.actions.GetMonthsA;
import be.bonamis.projects.umpire.business.actions.GetMonthsR;
import be.bonamis.umpire.ui.client.ClientFactory;
import be.bonamis.umpire.ui.client.mvp.BootView;
import be.bonamis.umpire.ui.client.places.BootPlace;
import be.bonamis.umpire.ui.client.places.DispoPlace;
import be.bonamis.umpire.ui.client.places.HistoryPlace;
import be.bonamis.umpire.ui.client.places.QuizPlace;
import be.bonamis.umpire.ui.client.places.WhoPlace;
import be.credoc.common.gwt.actions.client.ActionServiceWrapper;
import be.credoc.common.gwt.actions.client.OnResponseCommand;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class BootActivity extends BaseActivity<BootPlace> implements BootView.Presenter {

	private BootView bootView;
	private ActionServiceWrapper asw;

	public BootActivity(BootPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
		this.clientFactory = clientFactory;
		this.asw = clientFactory.getActionServiceWrapper();
		this.place = place;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		bootView = clientFactory.getBootView();
		if (place.getUmpire() != null) {
			bootView.setValues(place.getUmpire());
		}
		bootView.setPresenter(this);
		panel.setWidget(bootView.asWidget());
	}

	@Override
	public void searchClubs(String text) {
		GetClubsA<GetClubsR> getclubs = new GetClubsA<GetClubsR>();
		getclubs.setName(text);
		asw.execute(getclubs, new OnResponseCommand<GetClubsR>() {
			@Override
			public void execute() {
				bootView.updateClubList(response.getClubsDTO().getClubsList());
			}
		});

	}

	@Override
	public void dispoButtonClicked() {
		GetMonthsA<GetMonthsR> action = new GetMonthsA<GetMonthsR>();
		asw.execute(action, new OnResponseCommand<GetMonthsR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new DispoPlace("dispo", bootView.getUmpire(), response.getSetDates()));
			}
		});
	}

	@Override
	public void whoButtonClicked() {
		bootView.setValues(bootView.getUmpire());
		clientFactory.getPlaceController().goTo(new WhoPlace("who", bootView.getUmpire()));
	}

	@Override
	public void quizButtonClicked() {
		GetAllQuizA<GetAllQuizR> action = new GetAllQuizA<GetAllQuizR>();
		asw.execute(action, new OnResponseCommand<GetAllQuizR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new QuizPlace("quiz", bootView.getUmpire(), response.getQuizs()));
			}
		});
	}

	@Override
	public void historyButtonClicked() {
		GetGamesByUmpireA<GetGamesByUmpireR> action = new GetGamesByUmpireA<GetGamesByUmpireR>();
		action.setId(bootView.getUmpire().getId());
		asw.execute(action, new OnResponseCommand<GetGamesByUmpireR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new HistoryPlace("history", bootView.getUmpire(), response.getGames()));
			}
		});
	}
}
